/*
 * @Author: 李琪 liqi@lark2game.com
 * @Date: 2024-12-07 10:57:20
 * @LastEditors: 李琪 liqi@lark2game.com
 * @LastEditTime: 2024-12-10 15:26:30
 * @FilePath: /cramped-room/assets/Script/Runtime/DataManager.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { sys } from 'cc'
import Singleton from '../Base/Singleton'
import Levels, { ITile } from '../Config/Levels/LevelsIndex'
import { GMTools } from '../Utils/UtilsIndex'
class UserData {
  /** 新的章节阶段奖励已经解锁的章节 */
  chapterGroup: number[] = []

  public loadJson(jsData: any) {
    this.chapterGroup = GMTools.setIntArr(jsData, 'chapterGroup', null)
  }
}

export default class GameData extends Singleton {
  public static instance: GameData = new GameData()

  public static get Instance(): GameData {
    if (!GameData.instance) {
      GameData.instance = new GameData()
    }
    return GameData.instance
  }
  //地图相关
  mapInfo: Array<Array<ITile>>
  mapRowCount: number = 0
  mapColumnCount: number = 0
  //关卡相关
  _curLevel: number = 1
  totalNumLevel: number = Object.keys(Levels).length

  /**地图块大小 */
  _TILE_WIDTH: number = 55
  _TILE_HEIGHT: number = 55

  /**玩家位置相关 */
  _PlayerX: number = 0
  _PlayerY: number = 0
  _PlayerTargetX: number = 0
  _PlayerTargetY: number = 0

  /**背包系统格子数量 */
  _BagGrid_row: number = 5
  _BagGrid_column: number = 5

  set BagGrid_row(row: number) {
    this._BagGrid_row = row
  }
  get BagGrid_row(): number {
    return this._BagGrid_row
  }
  set BagGrid_column(column: number) {
    this._BagGrid_column = column
  }
  get BagGrid_column(): number {
    return this._BagGrid_column
  }
  set PlayerX(x: number) {
    this._PlayerX = x
  }
  get PlayerX(): number {
    return this._PlayerX
  }
  set PlayerY(y: number) {
    this._PlayerY = y
  }
  get PlayerY(): number {
    return this._PlayerY
  }
  set PlayerTargetX(x: number) {
    this._PlayerTargetX = x
  }

  get PlayerTargetX(): number {
    return this._PlayerTargetX
  }
  set PlayerTargetY(y: number) {
    this._PlayerTargetY = y
  }
  get PlayerTargetY(): number {
    return this._PlayerTargetY
  }
  set curLevel(level: number) {
    this._curLevel = level
  }
  get curLevel(): number {
    return this._curLevel
  }

  get TILE_WIDTH(): number {
    return this._TILE_WIDTH
  }
  get TILE_HEIGHT(): number {
    return this._TILE_HEIGHT
  }
  set TILE_WIDTH(width: number) {
    this._TILE_WIDTH = width
  }
  set TILE_HEIGHT(height: number) {
    this._TILE_HEIGHT = height
  }

  /**用户数据 */
  private m_userData: UserData = new UserData()
  public getData(name: string): string {
    // try {
    //   const encodeStr: string = cc.sys.localStorage.getItem(`${name}_e`)
    //   if (encodeStr?.length > 0) {
    //     const decodeStr = GMTools.AESDecode(encodeStr, false)
    //     // GConfig.logAble && DYTools.log(`GData getData for key:${name} new version encodeStr:${encodeStr} decodeStr:${decodeStr}`);
    //     return decodeStr
    //   }
    // } catch (e) {
    //   console.error(e)
    // }
    // 兼容老版本未加密的数据
    const rawStr = sys.localStorage.getItem(name)
    // GConfig.logAble && DYTools.log(`GData getData for key:${name} old version rawStr:${rawStr}`);
    return rawStr
  }

  private get _userDataName(): string {
    return 'SUD10' // + this.m_userData.openId
  }

  private get _localDataName(): string {
    return 'SLD10' // + this.m_userData.openId;
  }

  private get _battleDataName(): string {
    return `SBD10${this.getPlayerId()}`
  }
  public getPlayerId() {
    return '0'
  }

  private get _notSettledBattleDataName(): string {
    return `SNSBD10${this.getPlayerId()}`
  }
}
export const DataMgr = GameData.instance
